package com.ruoyi.jimu.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.jimu.domain.JmReport;
import com.ruoyi.jimu.mapper.JmReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jimu.mapper.JmReportTypeMapper;
import com.ruoyi.jimu.domain.JmReportType;
import com.ruoyi.jimu.service.IJmReportTypeService;

import javax.annotation.Resource;

/**
 * 报表类型管理Service业务层处理
 *
 * @author linyang
 * @date 2025-08-15
 */
@Service
public class JmReportTypeServiceImpl implements IJmReportTypeService {

    @Resource
    private JmReportTypeMapper jmReportTypeMapper;


    @Resource
    private JmReportMapper jmReportMapper;

    /**
     * 查询报表类型管理
     *
     * @param id 报表类型管理主键
     * @return 报表类型管理
     */
    @Override
    public JmReportType selectJmReportTypeById(Long id) {
        return jmReportTypeMapper.selectJmReportTypeById(id);
    }

    /**
     * 查询报表类型管理列表
     *
     * @param jmReportType 报表类型管理
     * @return 报表类型管理
     */
    @Override
    public List<JmReportType> selectJmReportTypeList(JmReportType jmReportType) {
        List<JmReportType> list = jmReportTypeMapper.selectJmReportTypeList(jmReportType);
        return fillParentsTitle(list);
    }

    /**
     * 新增报表类型管理
     *
     * @param jmReportType 报表类型管理
     * @return 结果
     */
    @Override
    public int insertJmReportType(JmReportType jmReportType) {
        jmReportType.setCreateTime(DateUtils.getNowDate());
        return jmReportTypeMapper.insertJmReportType(jmReportType);
    }

    /**
     * 修改报表类型管理
     *
     * @param jmReportType 报表类型管理
     * @return 结果
     */
    @Override
    public int updateJmReportType(JmReportType jmReportType) {
        jmReportType.setUpdateTime(DateUtils.getNowDate());
        return jmReportTypeMapper.updateJmReportType(jmReportType);
    }

    /**
     * 批量删除报表类型管理
     *
     * @param ids 需要删除的报表类型管理主键
     * @return 结果
     */
    @Override
    public int deleteJmReportTypeByIds(Long[] ids) {
        return jmReportTypeMapper.deleteJmReportTypeByIds(ids);
    }

    /**
     * 删除报表类型管理信息
     *
     * @param id 报表类型管理主键
     * @return 结果
     */
    @Override
    public int deleteJmReportTypeById(Long id) {
        return jmReportTypeMapper.deleteJmReportTypeById(id);
    }


    /**
     * 填充父级类型名称
     *
     * @param list
     * @return
     */
    private List<JmReportType> fillParentsTitle(List<JmReportType> list) {
        list.stream().forEach(x -> {
            if (ObjectUtil.isNotEmpty(x.getParentId())) {
                Long id = x.getParentId();
                JmReportType parentJimuReportType = jmReportTypeMapper.selectJmReportTypeById(id);
                if (ObjectUtil.isNotEmpty(parentJimuReportType)) {
                    x.setParentTitle(parentJimuReportType.getTitle());
                }
            }
        });
        return list;
    }


    /**
     * 填充父级类型名称
     *
     * @param jimuReportType
     * @return
     */
    private JmReportType fillParentTitle(JmReportType jimuReportType) {
        if (ObjectUtil.isNotEmpty(jimuReportType.getParentId())) {
            Long id = jimuReportType.getParentId();
            JmReportType parentJimuReportType = jmReportTypeMapper.selectJmReportTypeById(id);
            if (ObjectUtil.isNotEmpty(parentJimuReportType)) {
                jimuReportType.setParentTitle(parentJimuReportType.getTitle());
            }
        }
        return jimuReportType;
    }


    /**
     * 查询当前节点下子节点的数量
     *
     * @param id
     * @return
     */
    public Long selectChildrenCount(Long id) {
        LambdaQueryWrapper<JmReportType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JmReportType::getParentId, id);
        return jmReportTypeMapper.selectCount(lambdaQueryWrapper);
    }



    /**
     * 查询当前节点下包含的报表数量
     *
     * @param id
     * @return
     */
    public Long selectContainReportCount(Long id){
        LambdaQueryWrapper<JmReport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JmReport::getTypeId,id);
        return jmReportMapper.selectCount(lambdaQueryWrapper);
    }
}
